package vampirism.core.client;

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.ButtonGroup;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.List;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;



public class MenuSelect {
	
    public Window playerWindow;
    public Window infoWindow;
    public MainGame main;
    
    public final Button createButton;
    public final Button playButton;
    public final Button deleteButton;
    
    public String[] infoEntries = {"  Character information  ","","",""};
    public Button[] playerButtons; 
    
    public Image imageActor;
    public ScrollPane imagePane;
    
    public ButtonGroup buttonGroup = new ButtonGroup();
    
    public MenuSelect(final MainGame main){
    	
    	System.out.println("#CLIENT: showing create interface");
    	
    	this.main = main;
    	
    	buttonGroup.setMinCheckCount(1);
    	buttonGroup.setMaxCheckCount(1);
    	
    	createButton = new TextButton("New", main.skin);
    	playButton = new TextButton("  Play  ", main.skin);
    	deleteButton = new TextButton("Delete", main.skin);
    	
    	playerButtons = new Button[main.myPlayers.size()];
    	
	    for(int i = 0; i < main.myPlayers.size(); i++){
	    	int type = main.myPlayers.get(i).type;
	    	Image img = new Image(main.framesPlayer[21*type]); 
		    img.setScale(3);
		    img.setOriginX(4);
		    img.setOriginY(4);
	    	playerButtons[i] = new Button(img, main.skin, "toggle");
	    }
	    
	    Label titleLabel1 = new Label("Character Selection", main.skin);
	    imageActor = new Image(main.framesPlayer[1]);
	    
	    imagePane = new ScrollPane(imageActor,main.skin);
	    
	    List list = new List(infoEntries, main.skin);
        ScrollPane infoPane = new ScrollPane(list, main.skin);
        infoPane.setTouchable(Touchable.disabled);
        //SplitPane splitPane = new SplitPane(imageActor, infoPane, false, main.skin, "default-horizontal");
        
        setInfoText(0);
        
        playerWindow = new Window("", main.skin);
        playerWindow.setPosition(180, 410);
        playerWindow.defaults().spaceBottom(10);
        playerWindow.row();
	    for(int i = 0; i < playerButtons.length; i++){
	    	buttonGroup.add(playerButtons[i]);
	    	playerWindow.add(playerButtons[i]).expandX().colspan(1).minWidth(32).minHeight(32);
	    }
	    playerWindow.add(createButton);
	    playerWindow.setMovable(false);
	    playerWindow.pack();
	    
        infoWindow = new Window("", main.skin);
        infoWindow.defaults().spaceBottom(10);
        infoWindow.setSize(350, 250);
        infoWindow.row();
        infoWindow.add(titleLabel1).padTop(5).colspan(82);
        infoWindow.row();
        infoWindow.add(imagePane).minWidth(108).minHeight(108).center();
        infoWindow.add(infoPane).maxHeight(170).minWidth(178);
        infoWindow.row();
	    infoWindow.add(playButton).padBottom(10).colspan(82);
	    infoWindow.setMovable(false);
	    
	    infoWindow.setPosition((main.W-infoWindow.getWidth())/2.0f, (main.H-infoWindow.getHeight())/2.0f);
	    playerWindow.setPosition(infoWindow.getX(), infoWindow.getY()+250);
	    
	    for(int i = 0; i < playerButtons.length; i++){
	    	final int num = i;
	    	playerButtons[num].addListener(new ChangeListener() {
                public void changed (ChangeEvent event, Actor actor) {
                	if(playerButtons[num].isChecked()){
                		setInfoText(num);
                	}
                }
	    	});
	    }

	    createButton.addListener(new ChangeListener() {
	            public void changed(ChangeEvent event, Actor actor) {

	            	main.changeMenu(1);
	            }
		});
	    
	    playButton.addListener(new ChangeListener() {
            public void changed(ChangeEvent event, Actor actor) {
            	
            	int chosen = -1;
        	    for(int i = 0; i < playerButtons.length; i++){
        	    	if(playerButtons[i].isChecked()){
        	    		chosen = i;
        	    		break;
        	    	}
        	    }
        	    	

            	playButton.setDisabled(true);
            	main.clientSide.simulateSelect(chosen);
            }
	    });
	   
	    
	    
    }

	private void setInfoText(int buttonChosen) {
		if(main.myPlayers!=null && main.myPlayers.size()>0){
			
			imageActor = new Image(main.framesPlayer[21*main.myPlayers.get(buttonChosen).type]);
			imagePane.clear();
			imagePane.setWidget(imageActor);
		
			String playerName = main.myPlayers.get(buttonChosen).name;
			int playerLevel = main.myPlayers.get(buttonChosen).level;
			int playerClass = main.myPlayers.get(buttonChosen).type;
		
			infoEntries[1] = "Name: "+playerName;
			infoEntries[2] = "Level: "+playerLevel;
			infoEntries[3] = "Class: "+playerClass;
		}
	}
}
